# -*- coding: utf-8 -*-
#
#    pyqtsozluk, qtsozluk and pysozluk mix dictionary application
#    Copyright (C) 2007  pyqtsozluk authors
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#


import sys, locale, os
from gettext import translation
from core import pysozlukCore
from PyQt4 import QtCore, QtGui, uic
from pysozlukglobals import *
from settingswindow import *
from mainwindow import *
_ = translation('pyqtsozluk', fallback=True).ugettext


class TrayBase(QtGui.QWidget):
    def __init__(self, *args):
        QtGui.QWidget.__init__(self)

        self.tray = QtGui.QSystemTrayIcon(self)

        self.sw = 0
        self.ui = 0
        self.settings = QtCore.QSettings()
        self.updateCheck = self.settings.value("checkUpdates").toBool();

        self.trayMenu = QtGui.QMenu()
        self.action_translate = QtGui.QAction(QtGui.QIcon("/usr/share/pyqtsozluk/images/forward.png"), _("&Translate"), self.tray)
        self.action_sozluk = QtGui.QAction(QtGui.QIcon("/usr/share/pyqtsozluk/images/start.png"), _("Open &Gui"), self.tray)
        self.action_configuration = QtGui.QAction(QtGui.QIcon("/usr/share/pyqtsozluk/images/configure.png"), _("&Configuration"), self.tray)
        self.action_about = QtGui.QAction(QtGui.QIcon("/usr/share/pyqtsozluk/images/about.png"), _("&About"), self.tray)
        self.action_quit = QtGui.QAction(QtGui.QIcon("/usr/share/pyqtsozluk/images/quit.png"), _("&Quit"), self.tray)

        self.trayMenu.addAction(self.action_translate)
        self.trayMenu.addSeparator()
        self.trayMenu.addAction(self.action_sozluk)
        self.trayMenu.addSeparator()
        self.trayMenu.addAction(self.action_configuration)
        self.trayMenu.addAction(self.action_about)
        self.trayMenu.addAction(self.action_quit)

        self.connect(self.action_translate, QtCore.SIGNAL("triggered()"), self.translate)
        self.connect(self.action_sozluk, QtCore.SIGNAL("triggered()"), self.sozlukOpen)
        self.connect(self.action_configuration, QtCore.SIGNAL("triggered()"), self.pyqtSettings)
        self.connect(self.action_about, QtCore.SIGNAL("triggered()"), self.pyqtAbout)
        self.connect(self.action_quit, QtCore.SIGNAL("triggered()"), self.sozlukQuit)
        self.connect(self, QtCore.SIGNAL("messageClicked()"), self.popupClicked)

        self.tray.setContextMenu(self.trayMenu)
        self.tray.setIcon(QtGui.QIcon("/usr/share/pyqtsozluk/pyqtsozluktray.png"))
        self.tray.setToolTip(_("PyQtSozluk"))

        self.tray.show()

        if(self.updateCheck):
            print "Checking updates"
            import urllib2
            try:
                response = urllib2.urlopen(updateServer + 'latest.txt')
                latestversion = response.read()
                print "Latest:", latestversion, "  Current:", version

                if(latestversion > version):
                    print _("There's a higher version available !")
                    self.showMessage(_("PyQtSozluk"), _("There's a higher version available"), self.Information, 8000)
            except:
                if(self.settings.value("popupmessages").toBool()):
                    print _("An error occured while connecting to server")
                    self.showMessage(_("PyQtSozluk"), _("An error accured while connecting to server"), QtGui.QSystemTrayIcon.Information, 4000)


    def translate(self):
        self.proc = QtCore.QProcess()

        self.cb = QtGui.QApplication.clipboard()
        self.text = self.cb.text(self.cb.Selection)
        self.text = self.text.toLower()
        self.prog = "pyqtsozlukcli -s " + self.text + " -g -o"

        if self.text.length() > 60:
            self.prog = self.prog + " -e" # Error ..

        self.proc.start(self.prog)

    def pyqtSettings(self):
        if (self.sw):
            self.sw.show()
        else:
            self.sw = SettingsBase()
            self.sw.show()

    def pyqtAbout(self):
        self.aw = AboutBase()

    def popupClicked(self):
        import os
        os.system("kfmclient openURL " + sourceCode)

    def sozlukOpen(self):
        if (self.ui):
            self.ui.show()
        else:
            self.ui = mainWindow(self)
            self.core = pysozlukCore(self.ui)
            self.ui.makeConnections(self.core)

            if self.settings.contains("windowposition"):
                self.ui.move(self.settings.value("windowposition").toPoint())
            else:
                self.dw = QtGui.QDesktopWidget()
                self.ui.move((self.dw.screenGeometry(self.dw.primaryScreen())).width()-self.ui.width(), (self.dw.screenGeometry(self.dw.primaryScreen()).height())/2 )

    def sozlukQuit(self):
        sys.exit()


